package org.ouwh.oowiki;

import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ouwh.fw.db.SqlModule;
import org.ouwh.fw.model.data.Struct;
import org.ouwh.fw.model.service.Service;

public class ConfigService extends Service {
	private static Log log = LogFactory.getLog(ConfigService.class);
	public static final String DEBUG_SQL = "debug-sql";
	private volatile Properties configs;

	@Override
	protected void dumpToStruct(Struct struct) {
		Enumeration<?> e = configs.keys();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			String value = configs.getProperty(key);
			struct.addData(key, value);
		}
	}

	@Override
	protected void init(Properties properties) {
		this.configs = properties;
		SqlModule.setDebugMode(Boolean.parseBoolean(properties.getProperty(DEBUG_SQL)));
	}

	@Override
	protected void reset(Properties properties) throws Exception {
		this.configs = properties;
		SqlModule.setDebugMode(Boolean.parseBoolean(properties.getProperty(DEBUG_SQL)));
	}

	@Override
	protected void shutdown() throws Exception {
		log.info("Global Config stopped.");
	}

	@Override
	protected void startup() throws Exception {
		log.info("Global Config started.");
	}

	public String get(String name) {
		return configs.getProperty(name);
	}

}
